<template>
  <div class="mange">
    <el-dialog :title="operateType === 'add' ? '新增用户' : '更新用户'" :visible.sync="inShow">
      <commonform :forLable="opertateFormLable" :form="operateForm" :inline="true" ref="form">

      </commonform>

      <div slot="footer" class="dialog-foot">
        <el-button @click="inShow = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>

      </div>

    </el-dialog>

    <div class="manageheader">
      <el-button type="primary" @click="adduser">+新增</el-button>

      <commonform class="button" :forLable="formLable" :form="searchFrom" :inline="true" ref="form">
        <el-button type="primary" @click="getList(searchFrom.row)">搜索</el-button>
      </commonform>

      <comontable :tableData="tableData" :tableLable="tabelLable" :config="config" @page="getList()"
        @edit="edituser(row)" @del="deluser()">

      </comontable>

    </div>
  </div>
</template>

<script>

import commonform from "@/components/form.vue"
import comontable from "@/components/componenttable.vue"
import { getuerr } from '../../../api/data.js';
export default {


  name: 'MagementUser',
  components: {
    commonform,
    comontable
  },
  data() {
    return {
      operateType: 'add',
      inShow: false,
      opertateFormLable: [
        {
          model: 'name',
          label: '姓名',
          type: 'input'
        },
        {
          model: 'age',
          label: '年龄',
          type: 'input'
        },
        {
          model: 'sex',
          label: '性别',
          type: 'select',
          opts: [
            {
              label: '男',
              value: 1
            },
            {
              label: '女',
              value: 0
            },
          ]

        },
        {
          model: 'birth',
          label: '出生日期',
          type: 'date'
        },
        {
          model: 'addr',
          label: '地址',
          type: 'input'
        },
      ],
      operateForm: {
        name: '',
        addr: '',
        age: '',
        birth: '',
        sex: '',

      },
      formLable: [
        {
          model: "keyword",
          label: '',
          type: 'input'
        }

      ],
      searchFrom: {
        keyword: ''
      },
      tableData: [],
      tabelLable: [
        {
          prop: "name",
          label: "姓名"
        },
        {
          prop: "age",
          label: "年龄"
        },
        {
          prop: "sex",
          label: "性别"
        },
        {
          prop: "birth",
          label: "出生日期",
          width: 200
        },
        {
          prop: "addr",
          label: "地址",
          width: 320

        },

      ],
      config: {
        page: 2,
        total: 30
      }


    };
  },
  created() {
    this.getList()
  },
  mounted() {

  },

  methods: {
    confirm() {
      if (this.operateType === 'edit') {
        this.$http.post('/user/edit', this.operateForm).then(res => {
          console.log(res);
          this.inShow = false
          this.getList()
        })
      } else {
        this.$http.post('/user/add', this.operateForm).then(res => {
          console.log(res);
          this.inShow = false
          this.getList()
        })
      }
       this.operateForm = {
        name: '',
        addr: '',
        age: '',
        birth: '',
        sex: '',

      }

    },

    adduser() {
      this.inShow = true,
        this.operateType = 'add'
      this.operateForm = {
        name: '',
        addr: '',
        age: '',
        birth: '',
        sex: '',

      }

    },
    getuser() {

    },
    getList(name = '') {
      console.log("触发getlist函数");
      this.config.loading = true
      name ? (this.config.page = 1) : ''
      getuerr({
        page: this.config.page,
        name
      }).then(res => {
        console.log(res);
        this.tableData = res.data.list.map(item => {
          item.sex = (item.sex === 0 ? "女" : "男")
          return item
        })
        this.config.total = res.count
        this.config.loading = false
      })

    },
    edituser(row) {
      console.log("输出");
      console.log(row);
      this.operateType = "edit"
      this.inShow = true
      this.operateForm = row
      this.getList(row)
    },
    deluser() {
      this.$confirm("此操作将永久删除该文件,是否确定?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(row=> {
        const id = row.id
        this.$http.get("/user/del", {
          params: { id }
        }).then(res => {
          console.log(res);
          this.$message({
            type: 'success',
            massage: '删除成功'
          })
          this.getList()

        })
      })
    }
  },
};
</script>

<style lang="less" scoped>
.manageheader {
  width: 100%;




}

.button {
  display: inline;
  position: absolute;
  right: 50px;
}
</style>